package Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.InputStream;
import java.util.*;

/**
 * Created by 10188 on 2018/3/8.
 */
public class OCRUtil {
    private static final String host = "https://ocrapi-ecommerce.taobao.com";
    private static final String path = "/ocrservice/ecommerce";
    private static final String method = "POST";
    private static final String appcode = "e04deec181e94c079ff8bb482f02a6c6";

    public static String doOcr(String img) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String,Object>> wordslist = new ArrayList<Map<String,Object>>();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        //headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("typeId", "1991");
        Map<String, String> body = new HashMap<String, String>();
        //body.put("url", url);
        if (img.contains("http")) {
            body.put("url", img);
        }else {
            body.put("img", img);
        }
        String bodys = JSON.toJSONString(body);
        //String bodys = "{//图像数据：base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边最大4096px，支持jpg/png/bmp格式，和url参数只能同时存在一个\"img\":\"\",//图像url地址：图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4M，最短边至少15px，最长边最大4096px，支持jpg/png/bmp格式，和img参数只能同时存在一个\"url\":\"\",//是否需要识别结果中每一行的置信度，默认不需要。true：需要false：不需要\"prob\":false}";

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //System.out.println(response.toString());
            //获取response的body
            HttpEntity entity = response.getEntity();
            String entityString = EntityUtils.toString(entity);
            JSONObject content = JSON.parseObject(entityString);
            String sid=content.getString("sid");
            if (sid == null) {
                return null;
            }
            result.put("sid",sid);
            JSONArray wordsjsonarray = JSON.parseArray(content.getString("prism_wordsInfo"));
            ListIterator<Object> objectListIterator = wordsjsonarray.listIterator();
            while (objectListIterator.hasNext()) {
                JSONObject wordsjsonobject = (JSONObject) objectListIterator.next();
                Map<String,Object> wordsmap=new HashMap<String,Object>();
                List<Object> poslist = new ArrayList<Object>();
                wordsmap.put("word", wordsjsonobject.getString("word"));
                wordsmap.put("leftuppos",wordsjsonobject.getJSONArray("pos").get(3));
                wordslist.add(wordsmap);
            }
            result.put("wordslist", wordslist);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return   JSON.toJSONString(result);
    }
}
